package com.esri.core.ogc.wmts;

import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.tasks.e.a.g;
import com.esri.core.ogc.OwsDescription;
import com.esri.core.ogc.ResourceUrlInfo;
import com.esri.core.portal.WebMapLayer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.util.MinimalPrettyPrinter;
import org.xml.sax.Attributes;

/* loaded from: classes.dex */
public class WMTSLayerInfo extends OwsDescription {
    String a;
    String b;
    List<WMTSLayerInfo> c;
    private g d;
    private WMTSServiceInfo e;
    private WMTSStyle f;
    private Map<String, ResourceUrlInfo> g;
    private List<g> h;
    private Map<Integer, g> i;
    private Map<String, WMTSStyle> j;
    private ArrayList<String> k;
    private WMTSTileMatrixSetLink l;
    protected g latlonExtent;
    private Map<String, WMTSTileMatrixSetLink> m;
    protected g mCurWgs84Extent;
    private WMTSStyle n;

    public WMTSLayerInfo(WMTSServiceInfo wMTSServiceInfo) {
        this.a = null;
        this.b = null;
        this.latlonExtent = null;
        this.mCurWgs84Extent = null;
        this.f = null;
        this.g = new HashMap();
        this.h = new ArrayList();
        this.i = new HashMap();
        this.j = new HashMap();
        this.k = new ArrayList<>();
        this.m = new HashMap();
        this.e = wMTSServiceInfo;
    }

    public WMTSLayerInfo(WMTSServiceInfo wMTSServiceInfo, WebMapLayer webMapLayer) {
        this.a = null;
        this.b = null;
        this.latlonExtent = null;
        this.mCurWgs84Extent = null;
        this.f = null;
        this.g = new HashMap();
        this.h = new ArrayList();
        this.i = new HashMap();
        this.j = new HashMap();
        this.k = new ArrayList<>();
        this.m = new HashMap();
        this.e = wMTSServiceInfo;
        this.a = webMapLayer.getWmtsLayerId();
        WMTSStyle wMTSStyle = new WMTSStyle();
        this.n = wMTSStyle;
        this.j.put(wMTSStyle.getIdentifier(), this.n);
        this.k.add("dummy");
        WMTSTileMatrixSetLink wMTSTileMatrixSetLink = new WMTSTileMatrixSetLink();
        wMTSTileMatrixSetLink.a(webMapLayer.getWmtsTileMatrixSetId());
        this.m.put(webMapLayer.getWmtsTileMatrixSetId(), wMTSTileMatrixSetLink);
    }

    void a(WMTSLayerInfo wMTSLayerInfo) {
        if (this.c == null) {
            this.c = new ArrayList();
        }
        this.c.add(wMTSLayerInfo);
    }

    void a(String str) {
        this.a = str;
    }

    public String getIdentifier() {
        return this.a;
    }

    void b(String str) {
        this.b = str;
    }

    public WMTSServiceInfo getServiceInfo() {
        return this.e;
    }

    public List<g> getExtents() {
        return new ArrayList(this.i.values());
    }

    public g getExtentByWkid(int i) {
        return this.i.get(Integer.valueOf(i));
    }

    public List<g> getWgs84Extents() {
        return this.h;
    }

    public List<ResourceUrlInfo> getResourceUrls() {
        return new ArrayList(this.g.values());
    }

    public ResourceUrlInfo getResourceUrlByTypeAndFormat(String str, String str2) {
        Map<String, ResourceUrlInfo> map = this.g;
        return map.get(str + ":" + str2);
    }

    public List<String> getTileMatrixSetIds() {
        return new ArrayList(this.m.keySet());
    }

    public List<WMTSTileMatrixSetLink> getTileMatrixSetLinks() {
        return new ArrayList(this.m.values());
    }

    public WMTSTileMatrixSetLink getTileMatrixSetLinkById(String str) {
        return this.m.get(str);
    }

    public List<WMTSStyle> getStyles() {
        return new ArrayList(this.j.values());
    }

    public List<String> getStyleIds() {
        return new ArrayList(this.j.keySet());
    }

    public WMTSStyle getStyleById(String str) {
        return this.j.get(str);
    }

    public WMTSStyle getDefaultStyle() {
        if (this.n == null && !this.j.isEmpty()) {
            this.n = this.j.values().iterator().next();
        }
        return this.n;
    }

    public List<String> getFormats() {
        return this.k;
    }

    void a(g gVar) {
        this.latlonExtent = gVar;
    }

    @Override // com.esri.core.ogc.OwsItem
    public void startElement(String str, Attributes attributes) {
        super.startElement(str, attributes);
        if ("WGS84BoundingBox".equals(str)) {
            g gVar = new g();
            this.mCurWgs84Extent = gVar;
            gVar.a(g.a);
            this.mCurWgs84Extent.a(new Envelope());
        } else if ("BoundingBox".equals(str)) {
            g gVar2 = new g();
            this.d = gVar2;
            gVar2.a(new Envelope());
            b(attributes);
        } else if ("ResourceURL".equals(str)) {
            a(attributes);
        } else if ("Style".equals(str)) {
            String value = attributes.getValue("isDefault");
            boolean z = value != null && value.equals("true");
            WMTSStyle wMTSStyle = new WMTSStyle(z);
            this.f = wMTSStyle;
            if (z) {
                this.n = wMTSStyle;
            }
        } else if ("TileMatrixSetLink".equals(str)) {
            this.l = new WMTSTileMatrixSetLink();
        } else {
            WMTSTileMatrixSetLink wMTSTileMatrixSetLink = this.l;
            if (wMTSTileMatrixSetLink != null) {
                wMTSTileMatrixSetLink.startElement(str, attributes);
                return;
            }
            WMTSStyle wMTSStyle2 = this.f;
            if (wMTSStyle2 != null) {
                wMTSStyle2.startElement(str, attributes);
            }
        }
    }

    @Override // com.esri.core.ogc.OwsDescription, com.esri.core.ogc.OwsItem
    public void endElement(String str) {
        WMTSTileMatrixSetLink wMTSTileMatrixSetLink;
        String currentTagName = getCurrentTagName();
        if (this.l != null && !"TileMatrixSetLink".equals(currentTagName)) {
            this.l.endElement(str);
        } else if (this.f != null && !"Style".equals(currentTagName)) {
            this.f.endElement(str);
        } else if (currentTagName.equals("Identifier")) {
            d(this.mData.toString().trim());
        } else if (currentTagName.equals("LowerCorner")) {
            f(this.mData.toString());
        } else if (currentTagName.equals("UpperCorner")) {
            e(this.mData.toString());
        } else if (currentTagName.equals("WGS84BoundingBox")) {
            g gVar = this.mCurWgs84Extent;
            if (gVar != null) {
                this.h.add(gVar);
                this.mCurWgs84Extent = null;
            }
        } else if ("BoundingBox".equals(currentTagName)) {
            g gVar2 = this.d;
            if (gVar2 != null) {
                this.i.put(Integer.valueOf(g.b(gVar2.b())), this.d);
                this.d = null;
            }
        } else if ("Style".equals(currentTagName)) {
            WMTSStyle wMTSStyle = this.f;
            if (wMTSStyle != null) {
                this.j.put(wMTSStyle.getIdentifier(), this.f);
                this.f = null;
            }
        } else if ("Format".equals(currentTagName)) {
            this.k.add(this.mData.toString());
        } else if ("TileMatrixSetLink".equals(currentTagName) && (wMTSTileMatrixSetLink = this.l) != null) {
            this.m.put(wMTSTileMatrixSetLink.getTileMatrixSetIdentifier(), this.l);
            this.l = null;
        }
        super.endElement(str);
    }

    @Override // com.esri.core.ogc.OwsItem
    public void characters(char[] cArr, int i, int i2) {
        WMTSTileMatrixSetLink wMTSTileMatrixSetLink = this.l;
        if (wMTSTileMatrixSetLink != null) {
            wMTSTileMatrixSetLink.characters(cArr, i, i2);
        } else {
            WMTSStyle wMTSStyle = this.f;
            if (wMTSStyle != null) {
                wMTSStyle.characters(cArr, i, i2);
            }
        }
        super.characters(cArr, i, i2);
    }

    private void a(Attributes attributes) {
        String value = attributes.getValue("format");
        String value2 = attributes.getValue("resourceType");
        Map<String, ResourceUrlInfo> map = this.g;
        map.put(value2 + ":" + value, new ResourceUrlInfo(value, value2, attributes.getValue("template")));
    }

    private void b(Attributes attributes) {
        g gVar = this.d;
        if (gVar != null) {
            gVar.a(c(attributes.getValue("crs")));
        }
    }

    private String c(String str) {
        if (str.toLowerCase().contains("epsg")) {
            String[] split = str.split(":");
            int i = 0;
            try {
                i = Integer.parseInt(split[split.length - 1]);
            } catch (NumberFormatException unused) {
            }
            if (i == 900913) {
                i = SpatialReference.WKID_WGS84_WEB_MERCATOR_AUXILIARY_SPHERE_10;
            }
            if (i > 0) {
                return "EPSG:" + i;
            }
        }
        return "";
    }

    private void d(String str) {
        if (str == null || str.length() <= 0 || getStackSize() != 1) {
            return;
        }
        a(str);
    }

    private void e(String str) {
        Envelope a;
        g gVar = this.mCurWgs84Extent;
        if (gVar != null) {
            a = gVar.a();
        } else {
            g gVar2 = this.d;
            a = gVar2 != null ? gVar2.a() : null;
        }
        if (a != null) {
            String[] split = str.split(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR);
            a.setXMax(Double.parseDouble(split[0]));
            a.setYMax(Double.parseDouble(split[1]));
        }
    }

    private void f(String str) {
        Envelope a;
        g gVar = this.mCurWgs84Extent;
        if (gVar != null) {
            a = gVar.a();
        } else {
            g gVar2 = this.d;
            a = gVar2 != null ? gVar2.a() : null;
        }
        if (a != null) {
            String[] split = str.split(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR);
            a.setXMin(Double.parseDouble(split[0]));
            a.setYMin(Double.parseDouble(split[1]));
        }
    }

    public List<WMTSLayerInfo> getSubLayers() {
        return this.c;
    }
}
